num RPM_TABLE			= 0x7C68;		// Self explanetory
num RPMHI_TABLE			= 0x7C7C;		// vars with "_boost_" in their name are the new locations
num IGN_TABLE			= 0x7e0c;
num IGNHI_TABLE			= 0x7ED4;
num IGN_BOOST_TABLE		= 0x6B90;
num IGNHI_BOOST_TABLE	= 0x7060;
num FUEL_TABLE			= 0x7C9A;
num FUEL_BOOST_TABLE	= 0x6770;
num FUELHI_BOOST_TABLE	= 0x71F0;
num FUELHI_TABLE		= 0x7D6C;

num MBAR_TABLE			= 0x7C4A;		// map scaler table address
num TABLELO_X			= 10;			// dimentions of stock tables
num TABLELO_Y			= 20;			//
num TABLEHI_X			= 10;			//
num TABLEHI_Y			= 15;			//

num NEW_TABLELO_X			= 15;		// dimentions to convert respective tables to
num NEW_TABLELO_Y			= 20;		//
num NEW_TABLEHI_X			= 15;		//
num NEW_TABLEHI_Y			= 15;		//

num bOpen			= 0;
num LAST_MAP_VALUE		= 0x85;			// map value to assign to the last n/a map (column 9)
num VERSION				= 5;			// current uberdata version

num VERSION_BYTE		= 0x7A08;		// location in rom used to store uberdata version
num MAPSENSOR_FLOAT		= 0x7A09;		// location in rom to save current map sensor type


void DisableCRC()
{
	writebyte(0x2a1d+0, 0x03);
	writebyte(0x2a1d+1, 0x2a);
	writebyte(0x2a1d+2, 0x2a);
	writebyte(0x2a1d+3, 0x00);
}

void setlofuelx(num sz)					// sets width of low-cam fuel table in the rom
{
	writebyte(0x1267, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num getlofuelx()						// gets width of low-cam fuel table in the rom
{
	return readbyte(0x1267);
}

void setlofuely(num sz)					// sets height of low-cam fuel table in the rom
{
	writebyte(0x1283, sz);
	writebyte(0x0A59, sz-2);
}

num getlofuely()						// gets height of low-cam fuel table in the rom
{
	return readbyte(0x1283);
}

void sethifuelx(num sz)					// sets width of hi-cam fuel table in the rom
{
	writebyte(0x1267, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num gethifuelx()						// gets width of hi-cam fuel table in the rom
{
	return readbyte(0x1267);
}

void sethifuely(num sz)					// sets height of hi-cam fuel table in the rom
{
	writebyte(0x1269, sz);
	writebyte(0x0A71, sz-2);
}

num gethifuely()						// gets height of hi-cam fuel table in the rom
{
	return readbyte(0x1269);
}

void setloignx(num sz)					// sets width of lo-cam ign table in the rom
{
	writebyte(0x0B86, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num getloignx()							// gets width of lo-cam ign table in the rom
{
	return readbyte(0x0B86);
}

void setloigny(num sz)					// sets height of lo-cam ign table in the rom
{
	writebyte(0x0B88, sz);
	writebyte(0x0A59, sz-2);
}

num getloigny()							// gets height of lo-cam ign table in the rom
{
	return readbyte(0x0B88);
}

void sethiignx(num sz)					// sets width of hi-cam ign table in the rom
{
	writebyte(0x0B86, sz);
	writebyte(0x0AA9, sz-2);
	writebyte(0x0ABE, sz-2);
	writebyte(0x0AD3, sz-2);
}

num gethiignx()							// gets width of hi-cam ign table in the rom
{
	return readbyte(0x0B86);
}

void sethiigny(num sz)					// sets height of hi-cam ign table in the rom
{
	writebyte(0x0BA2, sz);
	writebyte(0x0A71, sz-2);
}

num gethiigny()							// gets height of hi-cam ign table in the rom
{
	return readbyte(0x0BA2);
}

void setlofuel(num addr)				// sets the location of the low-cam fuel table in the rom
{
	writeword(0x128B, addr);
}

num getlofuel()							// gets the location of the low-cam fuel table in the rom
{
	return readword(0x128B);
}

void sethifuel(num addr)				// sets the location of the hi-cam fuel table in the rom
{
	writeword(0x1277, addr);
}

num gethifuel()							// gets the location of the hi-cam fuel table in the rom
{
	return readword(0x1277);
}

void setloign(num addr)					// sets the location of the low-cam ign table in the rom
{
	writeword(0x64DF, addr);
}

num getloign()							// gets the location of the low-cam ign table in the rom
{
	return readword(0x64DF);
}

void sethiign(num addr)					// sets the location of the hi-cam ign table in the rom
{
	writeword(0x64E9, addr);
}

num gethiign()							// gets the location of the hi-cam ign table in the rom
{
	return readword(0x64E9);
}

void setlorpm(num addr)					// sets the location of the lo-cam RPM table in the rom
{
	writeword(0x0A50, addr);
}

num getlorpm()							// gets the location of the lo-cam RPM table in the rom
{
	return readword(0xA50);
}

void sethirpm(num addr)					// sets the location of the hi-cam RPM table in the rom
{
	writeword(0x0A68, addr);
}

num gethirpm()							// gets the location of the hi-cam RPM table in the rom
{
	return readword(0x0A68);
}

num setmapscl(num addr)					// sets the location of the map scaler table in the rom
{
	writeword(0x0AA3, addr);
	writeword(0x0AB8, addr);
	writeword(0x0ACD, addr);
}

num getmapscl()							// gets the location of the map scaler table in the rom
{
	return MBAR_TABLE;
}

void disablemapcheck()					// removes #A1 map check
{
	writebyte(0x0848, 0);
	writebyte(0x0849, 0);
}

void erasemap(num addr, num sz)
{
	num i = 0;
	num new_addr;
	while(i < sz)
	{
		new_addr = addr + i;
		writebyte(new_addr, 0xFF);
		i = i + 1;
	}
}

void copymap(num src_map, num sx, num sy, num dest_map, num dx, num dy) // copies and resizes from one map to another
{
	num x = 0;
	while( x < sx)
	{
		num y = 0;
		while(y < sy)
		{
			num src_byte = readbyte(src_map+(y*sx)+x);
			num dest_addr = dest_map+(y*dx)+x;
			writebyte(dest_addr, src_byte);
			y = y + 1;
		}
		x = x + 1;
	}
}

num open()										// called when a ROM is opened
{
	bOpen = 1;
	num version = readbyte(VERSION_BYTE);
	if(version == 0xFF)
	{
		DisableCRC();
		copymap(IGN_TABLE, TABLELO_X, TABLELO_Y+0, IGN_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
		copymap(IGNHI_TABLE, TABLEHI_X, TABLEHI_Y, IGNHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);

		copymap(FUEL_TABLE, TABLELO_X, TABLELO_Y+1, FUEL_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y+1);
		copymap(FUELHI_TABLE, TABLEHI_X, TABLEHI_Y+1, FUELHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y+1);

		setloignx(NEW_TABLELO_X);
		sethiignx(NEW_TABLEHI_X);

		setloigny(NEW_TABLELO_Y);
		sethiigny(NEW_TABLEHI_Y);

		setlofuelx(NEW_TABLELO_X);
		sethifuelx(NEW_TABLEHI_X);

		setlofuely(NEW_TABLELO_Y);
		sethifuely(NEW_TABLEHI_Y);

		setlofuel(FUEL_BOOST_TABLE);
		sethifuel(FUELHI_BOOST_TABLE);

		setloign(IGN_BOOST_TABLE);
		sethiign(IGNHI_BOOST_TABLE);

		writebyte(VERSION_BYTE, 5);

		setmapscl(MBAR_TABLE);
	}
	disablemapcheck();

	NEW_TABLEHI_Y = gethifuely();
}

num save()									// called right before a rom is saved
{
}

num close()									// called when a rom is closed
{
	bOpen = 0;
}

